/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Customer;

import javax.persistence.EntityManager;
import javax.swing.JOptionPane;
import model.Customer;
import supermarket.DBmanager;
import supermarket.MainForm;
import supermarket.SuperMarket;

/**
 *
 * @author aggelos
 */
public class ChangePasswordPanel extends javax.swing.JPanel {

    private DBmanager db;
    private EntityManager em;
    private Customer user;
    private MainForm parent;
    private String OldPassword;

    /**
     * Creates new form ChangePasswordPanel
     */
    public ChangePasswordPanel(DBmanager db, MainForm parent) {
        this.db = db;
        this.parent = parent;
        this.user = this.parent.GetUser();
        

        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        oldPass = new javax.swing.JPasswordField();
        newPass = new javax.swing.JPasswordField();
        newPassConfirm = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        b_ChangePassWord = new javax.swing.JButton();

        jPanel1.setBackground(new java.awt.Color(248, 127, 7));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setText("Αλαγή Κωδικού");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        oldPass.setText("jPasswordField1");

        newPass.setText("jPasswordField1");

        newPassConfirm.setText("jPasswordField1");

        jLabel2.setText("Παλιός Κωδικός");

        jLabel3.setText("Νέος Κωδικός");

        jLabel4.setText("Επιβεβαίωση Κωδικού");

        b_ChangePassWord.setText("Αλαγή Κωδικού");
        b_ChangePassWord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_ChangePassWordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newPassConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(b_ChangePassWord))
                    .addComponent(newPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(oldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(oldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newPassConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(b_ChangePassWord))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
/***** Αλαγή Κωδικού Χρήστη***************/
    private void b_ChangePassWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_ChangePassWordActionPerformed
        try {
            
            this.OldPassword = this.user.getPassword();
            
            if (!this.OldPassword.equals(String.valueOf(this.oldPass.getPassword()))) {
                JOptionPane.showMessageDialog(this, SuperMarket.msgWrongOldPass, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
                return;
            }
            if(!String.valueOf(this.newPass.getPassword()).equals(String.valueOf(this.newPassConfirm.getPassword()))){
                            JOptionPane.showMessageDialog(this, SuperMarket.msgWrongConfPass, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
                            return;
            }
            
                    
              if(String.valueOf(this.newPass.getPassword()).equals("") || String.valueOf(this.newPassConfirm.getPassword()).equals("") ){
                            JOptionPane.showMessageDialog(this, SuperMarket.msgEmptyPass, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
                            return;
            }         
            
            
            this.user.setPassword(String.valueOf(this.newPass.getPassword()));
            this.db.updateCustomer(user);
            
            JOptionPane.showMessageDialog(this, SuperMarket.msgChangePassSucc, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);

        }

    }//GEN-LAST:event_b_ChangePassWordActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_ChangePassWord;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField newPass;
    private javax.swing.JPasswordField newPassConfirm;
    private javax.swing.JPasswordField oldPass;
    // End of variables declaration//GEN-END:variables
}
